<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Skills
 *
 * @author Willie
 */
class Message extends CI_Model{
    
    private $MSG_TABLE="messages";
    
    
    function __construct() {
        parent::__construct();
    }
    
    function addMessage($uid,$subject,$body,$read){
          $escapedSubject = $this->db->escape($subject);
          $escapedBody=$this->db->escape($body);
        $sql = "INSERT INTO ".$this->MSG_TABLE." (TO_USER,MSG_SUBJ,MSG_BODY,MSG_SENT_TIME,MSG_READ_YN) VALUES ".
                "($uid,$escapedSubject,$escapedBody,now(),'$read')";
        
       $success= $this->db->simple_query($sql);
       if($success){
           return $success;
       }else{
           log_message('error',$this->db->_error_message());
       }
       
    }
    
    function getAllMessagesByUID($uid){
        if($uid!=NULL && $uid!=""){
            $sql = "SELECT * FROM ".$this->MSG_TABLE." WHERE MSG_READ_YN='N' AND TO_USER=".$uid;
            $sql.=" ORDER BY MSG_SENT_TIME DESC LIMIT 10";
            $rs= $this->db->query($sql);
            if($rs!=NULL){
                return $rs->result();
            }
        }
        return null;
    }
    
  

    
    
    
}